package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.SmsCheckInfoBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MobileDesUtils;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/5/25 0025
 */
public class ChangeNewPwdActivity extends BaseSecondActivity {


    @BindView(R.id.changenewpwd_head)
    ExpandHeadView mChangenewpwdHead;
    @BindView(R.id.newpwd)
    EditText mNewpwd;
    @BindView(R.id.confrimnewpwd)
    EditText mConfrimnewpwd;
    @BindView(R.id.next)
    Button mNext;


    @Override
    protected void initView() {
        String dowhat = getIntent().getStringExtra("dowhat");
        if (TextUtils.equals(dowhat, "changePwd")) {
            mChangenewpwdHead.getHeadViewMidText().setText("修改密码");
        } else {
            mChangenewpwdHead.getHeadViewMidText().setText("忘记密码");
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_changenewpwd;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mChangenewpwdHead;
    }


    @OnClick(R.id.next)
    public void onViewClicked() {
        String phoneNumber = getIntent().getStringExtra("phoneNumber");
        String newPassword = mNewpwd.getText().toString().trim();
        String newConfirmPassword = mConfrimnewpwd.getText().toString().trim();
        if (TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(newConfirmPassword)) {
            MyToast.show("密码不能为空");

        } else if (!TextUtils.equals(newPassword, newConfirmPassword)) {
            MyToast.show("两次密码不一致");

        } else {
            processData(phoneNumber, newPassword);

        }
    }

    /**
     * 修改密码操作
     * @param loginName 登陆密码
     * @param newPassword  新的密码
     */
    private void processData(String loginName, String newPassword) {
        OkHttpUtils.post().url(Constants.UPDATEPASSWORD_URL)
                .addParams("loginName", loginName)
                .addParams("loginPwd", MobileDesUtils.strEnc(newPassword, Constants.FIRSTKEY, null, null))
                .addParams("sign", Constants.SIGN)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                SmsCheckInfoBean smsCheckInfoBean = new Gson().fromJson(response, SmsCheckInfoBean.class);
                if (smsCheckInfoBean.getSuccess() == 0) {
                    MyToast.show(smsCheckInfoBean.getMessage());
                    startActivity(new Intent(UIUtils.getContext(), ChangePwdSuccessActivity.class));
                    finish();
                } else {
                    MyToast.show(smsCheckInfoBean.getMessage());
                }


            }
        });
    }
}
